package com.wardrobe.soa.backend.goods_order;

import com.google.common.collect.Maps;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.soa.backend.booking_order.BackBookingOrderFromBean;
import com.wardrobe.soa.backend.booking_order.BackBookingOrderResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/23.
 */
@RestController
@RequestMapping("/backend/goodsorder")
public class BackGoodsOrderController {
    private static final Logger logger = LoggerFactory.getLogger(BackGoodsOrderController.class);

    @Autowired
    private BackGoodsOrderService backGoodsOrderService;

    @RequestMapping("/getGoodsOrderList")
    public Map<String, Object> getGoodsOrderList(BackGoodsOrderFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        List<BackGoodsOrderResponse> list = backGoodsOrderService.getGoodsOrderList(fromBean);
        list.stream().forEach(goodsOrder -> {
            goodsOrder.setCreateTime(goodsOrder.getCreateTime().substring(0, goodsOrder.getCreateTime().length() - 2));
            goodsOrder.setPayTime(goodsOrder.getPayTime().substring(0, goodsOrder.getPayTime().length() - 2));
        });
        long count =backGoodsOrderService.countGoodsOrder(fromBean);
        int pageCount = (int) Math.ceil(((double) count / fromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", fromBean.getPageSize());
        return result;
    }

    @RequestMapping("/modifyGoodsOrder")
    public Map<String, Object> modifyGoodsOrder(BackEndFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status = backGoodsOrderService.modifyGoodsOrder(fromBean);
            if (status == 1) {
                result.put("status", status);
                result.put("msg", "修改成功");
            } else {
                result.put("status", 2);
                result.put("msg", "修改失败");
            }
            return result;
        } catch (Exception e) {
            result.put("status", 2);
            result.put("msg", "修改失败");
            logger.error("modifyGoodsOrder error", e);
        }
        return result;
    }
}
